package com.atguigu.tingshu.album.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.file.FileNameUtil;
import cn.hutool.core.util.IdUtil;
import com.atguigu.tingshu.album.config.MinioConstantProperties;
import com.atguigu.tingshu.album.service.FileUploadService;
import com.atguigu.tingshu.album.service.VodService;
import com.atguigu.tingshu.common.execption.GuiguException;
import io.minio.MinioClient;
import io.minio.PutObjectArgs;
import io.minio.errors.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

/**
 * @author: atguigu
 * @create: 2025-03-07 15:24
 */
@Slf4j
@Service
public class FileUploadServiceImpl implements FileUploadService {

    @Autowired
    private MinioClient minioClient;

    @Autowired
    private MinioConstantProperties minioConstantProperties;

    @Autowired
    private VodService vodService;

    /**
     * 文件上传
     *
     * @param file
     * @return
     * @throws IOException
     */
    @Override
    public String uploadFile(MultipartFile file) {
        try {
            //1.校验图片文件是否符合要求 避免格式错误图片：txt>.png
            BufferedImage bufferedImage = ImageIO.read(file.getInputStream());
            if (bufferedImage == null) {
                throw new GuiguException(500, "图片文件不合法");
            }
            //2.校验文件尺寸：超过限制尺寸 900*900
            int height = bufferedImage.getHeight();
            int width = bufferedImage.getWidth();
            if (height > 900 || width > 900) {
                throw new GuiguException(500, "图片尺寸不合法");
            }

            //TODO 对图片内容审核
            String suggestion = vodService.scanImage(file);
            if ("block".equals(suggestion) || "review".equals(suggestion)) {
                throw new GuiguException(500, "图片内容违规");
            }

            //3.生成全局唯一的文件名称=/当日日期/随机名称.后缀  a.png
            String folderName = DateUtil.today();
            String fileName = IdUtil.randomUUID();
            String extName = FileUtil.extName(file.getOriginalFilename());
            String objectName = "/" + folderName + "/" + fileName + "." + extName;

            //4.上传到minio中
            minioClient.putObject(
                    PutObjectArgs.builder().bucket(minioConstantProperties.getBucketName()).object(objectName).stream(
                                    file.getInputStream(), file.getSize(), -1)
                            .contentType((file.getContentType()))
                            .build());
            //5.返回在线文件地址，用于预览
            return minioConstantProperties.getEndpointUrl() + "/" + minioConstantProperties.getBucketName() + objectName;
        } catch (Exception e) {
            log.error("[专辑服务]文件上传失败：{}", e);
            throw new GuiguException(500, e.getMessage());
        }
    }
}
